/*******************************************
 *                                         *
 *******************************************/

#include "Game.h"

namespace DirectXFramework
{
	Game::Game (void)
	{
		this->GameWindow = NULL;
		this->Renderer = NULL;
		this->Keyboard = NULL;
	}

	Game::Game (HINSTANCE Instance, LPCWSTR WindowTitle, UINT Width, UINT Height, BOOL FullScreen)
	{
		this->GameWindow = NULL;
		this->Renderer = NULL;
		this->Keyboard = NULL;
		this->Init(Instance, WindowTitle, Width, Height, FullScreen);
	}

	Game::~Game (void)
	{
		if (this->Mouse)
			delete this->Mouse;
		if (this->Keyboard)
			delete this->Keyboard;
		if (this->Renderer)
			delete this->Renderer;
		if (this->GameWindow)
			delete this->GameWindow;
	}

	BOOL Game::Init (HINSTANCE Instance, LPCWSTR WindowTitle, UINT Width, UINT Height, BOOL FullScreen)
	{
		BOOL Success = TRUE;

		if (!(this->GameWindow = new Window(Instance, WindowTitle, Width, Height, FullScreen)))
			Success = FALSE;
		else
		{
			if (!(this->Renderer = new GraphicsDevice(*GameWindow)))
				Success = FALSE;
			else
			{
				if (!(this->Keyboard = new GameKeyboard(GameWindow->GetWindowInstance(), GameWindow->GetWindowHandle())))
					Success = FALSE;
				if (!(this->Mouse = new GameMouse(GameWindow->GetWindowInstance(), GameWindow->GetWindowHandle())))
					Success = FALSE;
				this->GameWindow->Show();
			}
		}

		return Success;
	}

	BOOL Game::Update (void)
	{
		if (this->IsRunning = this->GameWindow->UpdateWindowMsg())
		{
			this->Renderer->BeginRendering();
			this->Renderer->EndRendering();
			this->Keyboard->Update();
			this->IsRunning = !this->Keyboard->IsKeyDown(DIK_ESCAPE);
		}
		return this->IsRunning;
	}
};